#include "main.h"
#include "Global.h"
#include "Functions.h"
#include "CAssault.h"
#include "Bullet.h"

CAssault::CAssault(){}

CAssault::CAssault(bool _equipped, CEntity* _parent){
	Init(NULL);
	m_ammo=30;
	m_maxammo=300;
	m_clipsize=30;
	m_scale.x=1;
	m_scale.y=1;
	m_dir=1;
	m_pSprite = g_pSpriteList->Weapon->AssaultRifle;

	m_pCollider->SetCollisionType(NoCollide);

	m_bEquipped = _equipped;
	if (m_bEquipped)
		m_pParent=_parent;
}

CAssault::~CAssault(){

}

/*////////////
Creates a bullet of type X, moving towards the mouse
removes 1 from the ammo count
function will fail if ammo count is 0
*////////////
CEntity* CAssault::Shoot(){
	if (m_ammo>0){
		Bullet* bul = new Bullet(this,atan2(MOUSE.y-m_pos.y,MOUSE.x-m_pos.x),20);
		bul->setPos(m_pos);
		bul->m_xs = (int)m_pos.x;
		bul->m_ys = (int)m_pos.y;
		return bul;
	}else
		return NULL;
}

void CAssault::Step(CEntityList *_EntList, BYTE _keys[256], DIMOUSESTATE _mouse){
	if (!m_bEquipped){
		Verlet();
		
		Node<CEntity>* it = _EntList->first();
		CEntity* Entity;
		bool collision=false;
		m_friction=V(0.02f, 0.02f);
		while(it!=NULL){
			Entity = it->object;

			//COLLISIONS
			if(Entity!=this)
			if(Entity->Solid || !Entity->Invincible){
					float Al,Ar,At,Ab,Asxh,Asyh,Axh,Ayh; // player
					float Bl,Br,Bt,Bb,Bsxh,Bsyh,Bxh,Byh; // brush
					
					float dx,dy,px,py; // difference
					
					//Axv = xv; Ayv = yv;
					//Bxv = Entity.x; Byv = Entity.y();
					
					Al = m_pos.x; Ar = m_pos.x+m_width; 
					At = m_pos.y; Ab = m_pos.y+m_height;
					Bl = Entity->getPos().x; Br = Entity->getPos().x+Entity->getWidth(); 
					Bt = Entity->getPos().y; Bb = Entity->getPos().y+Entity->getHeight();
					
					// halfwidths
					Asxh = (float)m_halfwidth;
					Asyh = (float)m_halfheight;
					Ayh = m_pos.y + (m_height/2);
					Axh = m_pos.x + (m_width/2);
					
					Bsxh = (float)Entity->getWidth() / 2;
					Bsyh = (float)Entity->getHeight() / 2;
					Byh = Bt + Bsyh;
					Bxh = Bl + Bsxh;
					
					// check if is inside
					if( Ar >= Bl && Al <= Br && Ab >= Bt && At <= Bb ){
									
						// difference in halfwidths
						dx = Axh-Bxh;
						// x penetration
						px = (Asxh+Bsxh)-abs(dx);
					
						if(px>0){
						
							dy = Ayh-Byh;
							// y penetration
							py = (Asyh+Bsyh)-abs(dy);		
							// both x and y penetration
							if(py>0){
								// does x have less penetration?
								if(px<py){
									// move left
									if(dx<0){
										m_pos.x -= px;
										//friction.y+=0.01;
									}
									else{					
										m_pos.x += px; // right
										//friction.y+=0.01;
									}
								}else{ // more y penetration
									if(dy<0){
										m_pos.y -= py;
										//friction.x+=0.015;
									}
									else{
										m_pos.y += py; // down
										//friction.x+=0.015;
									}
								}
								collision=true;
							}
						}
					}
			}
			it = it -> next;
		}
	}
}



void CAssault::Draw(Sprites* _Sprites){
	DrawSprite(m_pSprite,-1,m_pos, V(m_scale.x*m_dir, m_scale.y), 0, CLR_WHITE);
}